Ext.define("HRMobile.controller.LeaveController", {
    extend: "Ext.app.Controller",
    config: {
        refs: {
        	experienceList: 'list#leaverequestlist',
        	btnApprove : '#btnApprove',
        	btnReject: '#btnReject',
        	txtAreaComment: '#txtAreaComment',
        	yourleavelist :'list#yourleaverequestlist',
        	btnAdd: '#btnAdd'
        },
        control:{
        	experienceList: {
            	itemtap: function(list, index, target, record) {
            		var detail = Ext.getCmp('leaverequestdetails');
            		detail.reset();
            		detail.setRecord(record);
            		detail.show();
            	}
            },
            yourleavelist: {
            	itemtap: function(list, index, target, record) {
            		var detail = Ext.getCmp('yourleaverequestdetails');
            		detail.reset();
            		detail.setRecord(record);
            		detail.show();
            	}
            },
            btnAdd : {
            	tap: function() {
            		var detail = Ext.getCmp('yourleaverequestadd');
            		detail.show();
            	}
            },
            btnApprove: {
            	tap: function(){
            		var comment = this.getTxtAreaComment().getValue();
            		Ext.Msg.confirm('Confirmation','Approve this leave request?',
        				function(buttonId) 
            				{
			        			if(buttonId =='yes') 
			        				leaveUpdate(1,comment);
		        			});
            		
            	}
            },
            btnReject: {
            	tap: function(){
            		var comment = this.getTxtAreaComment().getValue();
            		Ext.Msg.confirm('Confirmation','Reject this leave request?',
        				function(buttonId)
            				{
		            			if(buttonId =='yes') 
		        					leaveUpdate(0,comment);
            				});
            		
            	}
            }
        }
      },
});
var leaveUpdate = function(approved,comment){
	Ext.Ajax.request({
        url: 'LeaveUpdateServlet',
        params: {
        	approved: approved,
        	comment: comment,
        	id: Ext.getCmp('leaverequestdetails').getRecord().data.id
        },
        success: function(response) {
        	Ext.getCmp('leaverequestdetails').hide();
        	Ext.getStore('LeaveRequestStore').load();
        }
	});
};